Partial Class Modules_Questionnaires_QuestionManager
    Inherits baseWebPage

    Protected Function GetCategory(ByVal id As Integer) As String
        Return DotNetPm.Business.Questionnaire.CategoryODS.GetCategory(id)
    End Function

    Protected Sub gvQuestions_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvQuestions.RowCommand
        Select Case e.CommandName
            Case "EditQuestion"
                ' Convert the row index stored in the CommandArgument
                ' property to an Integer.
                Dim index As Integer = Convert.ToInt32(e.CommandArgument) _
                    - gvQuestions.PageIndex * gvQuestions.PageSize
                ' Retrieve the row that contains the button clicked 
                ' by the user from the Rows collection.
                Dim row As GridViewRow = gvQuestions.Rows(index)
                MyBase.SetPreviousPage("page=" & gvQuestions.PageIndex)
                Response.Redirect("QuestionCreate.aspx?qid=" & row.Cells(0).Text)
            Case "PreviewQuestion"
                Dim index As Integer = Convert.ToInt32(e.CommandArgument) _
                    - gvQuestions.PageIndex * gvQuestions.PageSize
                Dim row As GridViewRow = gvQuestions.Rows(index)
                MyBase.SetPreviousPage("page=" & gvQuestions.PageIndex)
                Response.Redirect("QuestionPreview.aspx?qid=" & row.Cells(0).Text)
        End Select
    End Sub

    Protected Sub lbutNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbutNew.Click
        MyBase.SetPreviousPage("page=" & gvQuestions.PageIndex)
        'Response.Write(index)
        Response.Redirect("QuestionCreate.aspx")
    End Sub


#Region "Search Question"

    Protected Sub butSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butSearch.Click
        Session("_questionnaire_category") = ddlCategories.SelectedValue
    End Sub

    Protected Sub butClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butClear.Click
        ddlCategories.SelectedIndex = 0
        Session("_questionnaire_category") = 0
        txtStringToSearch.Text = String.Empty
    End Sub

    Protected Sub ddlCategories_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCategories.DataBound
        'Response.Write("ddlCategories_DataBound<br/>")
        ddlCategories.SelectedValue = Session("_questionnaire_category")
    End Sub

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack Then
            Exit Sub
        End If
        Dim pageIndex As String = Request.QueryString("page")
        If String.IsNullOrEmpty(pageIndex) Then
            Exit Sub
        End If
        Try
            gvQuestions.PageIndex = CInt(pageIndex)
        Catch ex As Exception
            Exit Sub
        End Try
    End Sub

    Protected Sub iButInstrument_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles iButInstrument.Click
        Response.Redirect("InstrumentManager.aspx")
    End Sub

    Protected Sub iButQuestionnaire_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles iButQuestionnaire.Click
        Response.Redirect("QuestionnaireManager.aspx")
    End Sub

    Protected Function IsAllowedToDelete() As Boolean
        If User.IsInRole("QuestionnaireAdmins") Then
            Return True
        End If
        Return False
    End Function
End Class
